﻿using MDK.Common;
using MDK.Model.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform
{
    public partial class frmPositionConfig : BaseForm
    {
        private T_Positionsetting _set { get; set; }

        public frmPositionConfig()
        {
            InitializeComponent();
        }

        private void frmPositionConfig_Load(object sender, EventArgs e)
        {
            InitPosition();
            BingData();
        }

        /// <summary>
        /// 初始工位选择
        /// </summary>
        public void InitPosition()
        {
            Dictionary<int, string> dic1 = new Dictionary<int, string>();
            for (int i = 1; i <= 40; i++)
            {
                if (i == 13 || i == 17 || i == 24 || i == 28)
                    continue;
                var positive = i.ToString().PadLeft(2, '0');
                cmbPositive.Properties.Items.Add(positive);
                cmbNobarCode.Properties.Items.Add(positive);
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BingData()
        {
            string url = string.Format("Positionsetting/Find?jsonWhere={0}", "");
            _set = WebAPIHelper.Get<T_Positionsetting>(url);
            if (_set == null)
            {
                _set = new T_Positionsetting();
                return;
            }
            if (_set.NoBarCode != null && _set.NoBarCode.Length > 2)
                cmbNobarCode.EditValue = _set.NoBarCode?.Substring(_set.NoBarCode.Length - 2, 2);
            if (_set.Positive != null && _set.Positive.Length > 2)
            {
                var positives = _set.Positive?.Split(',');
                var positivestr = "";
                foreach (var positive in positives)
                {
                    positivestr += positive.Substring(positive.Length - 2, 2) + ",";
                }
                cmbPositive.EditValue = positivestr.TrimEnd(',');
            }
        }


        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(_set.Id))
                {
                    _set.Id = Guid.NewGuid().ToString("N");
                    GetData();
                    WebAPIHelper.Post<T_Positionsetting>("Positionsetting/Add", _set);
                }
                else
                {
                    GetData();
                    WebAPIHelper.Post<T_Positionsetting>("Positionsetting/Modify", _set);
                }
                FastMessageBox.Show("编辑成功");
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                FastMessageBox.Error(ex.Message);
            }
        }

        public void GetData()
        {
            string url = string.Format("Position/GetPosition?end={0}&proType={1}", cmbNobarCode.Text.Trim().PadLeft(2, '0'), 2);
            var position = WebAPIHelper.Get<T_Position>(url);
            _set.NoBarCode = position?.Id;
            var positives = cmbPositive.Text.Trim().Split(',');
            string positivestr = "";
            foreach (var positive in positives)
            {
                url = string.Format("Position/GetPosition?end={0}&proType={1}", positive.Trim().PadLeft(2, '0'), 2);
                position = WebAPIHelper.Get<T_Position>(url);
                if (position != null)
                    positivestr += position.Id + ",";
            }
            _set.Positive = positivestr.TrimEnd(',');
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            DialogResult = DialogResult.Cancel;
        }

        private void cmbPositive_EditValueChanged(object sender, EventArgs e)
        {
            if ((cmbPositive.EditValue == null ? "" : cmbPositive.EditValue.ToString()).IndexOf(cmbNobarCode.EditValue == null ? "" : cmbNobarCode.EditValue.ToString()) >= 0)
            {
                cmbPositive.EditValue = null;
            }
        }

        private void cmbNobarCode_EditValueChanged(object sender, EventArgs e)
        {
            if ((cmbPositive.EditValue == null ? "" : cmbPositive.EditValue.ToString()).IndexOf(cmbNobarCode.EditValue == null ? "" : cmbNobarCode.EditValue.ToString()) >= 0)
            {
                cmbNobarCode.EditValue = null;
            }
        }
    }
}
